package org.example.spring.poi.service.impl;

import lombok.RequiredArgsConstructor;
import org.example.spring.poi.model.entity.SysUser;
import org.example.spring.poi.service.CommonTransactionService;
import org.example.spring.poi.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * @author hzq
 * @since 2022/6/16
 */
@Service
@RequiredArgsConstructor
public class CommonTransactionServiceImpl implements CommonTransactionService {

    private final SysUserService sysUserService;
    private final PlatformTransactionManager transactionManager;

    @Override
    public SysUser add(SysUser sysUser) {
        return sysUserService.add(sysUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUser addTransactional(SysUser sysUser) {
        System.out.println(TransactionSynchronizationManager.getCurrentTransactionName());
        SysUser sysUser1 = sysUserService.addTransactional(sysUser);
        System.out.println(TransactionSynchronizationManager.getCurrentTransactionName());
        return sysUser1;
    }
}
